package com.bootcamp.servlet.mvc.utils;

import com.bootcamp.servlet.mvc.model.Product;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @program: wrok-1101
 * @description:
 * @author: Keyu Li
 * @create: 2021-11-08 14:29
 **/

public class StoreUtils {
    public static void printStoreProducts(HttpServletRequest request, HttpServletResponse response) throws IOException {
        PrintWriter writer = response.getWriter();
        // 判断登录状态并写入首行
        PrintHeadByAuthUtils.printHead(request,writer);
        // 展示商品
        List<Product> products = (List<Product>) request.getServletContext().getAttribute("products");
        writer.println("<div><table>");
        // 第一行
        writer.println("        <tr style=\"font-size: larger\">\n" +
                "            <td>品牌</td>\n" +
                "            <td>颜色</td>\n" +
                "            <td>类别</td>\n" +
                "            <td>价格</td>\n" +
                "            <td>描述</td>\n" +
                "            <td></td>\n" +
                "        </tr>");
        // 内容
        for (Product p : products) {
            String href = request.getContextPath() + "/store/detail?id=" + p.getId();
            writer.println("<tr>\n" +
                    "                <td>" + p.getName() + "</td>\n" +
                    "                <td>" + p.getColor() + "</td>\n" +
                    "                <td>" + p.getCategory() + "</td>\n" +
                    "                <td>" + p.getPrice() + "</td>\n" +
                    "                <td>" + p.getDescription() + "</td>\n" +
                    "                <td><a href='" + href + "'>详情</a></td>\n" +
                    "        </tr>");
        }
        writer.println("</table></div>");
    }

    public static void printProductDetails(HttpServletRequest request, HttpServletResponse response) throws IOException {
        PrintWriter writer = response.getWriter();
        // 获取商品信息
        Integer id = Integer.parseInt(request.getParameter("id"));
        List<Product> products = (List<Product>) request.getServletContext().getAttribute("products");
        Product pdetail = null;
        for (Product product : products) {
            if (product.getId().equals(id)) {
                pdetail = product;
                break;
            }
        }
        // 如果瞎输入的id,找不到该商品
        if (pdetail == null) {
            writer.println("<h2>Illegal product id!</h2>");
            return;
        }
        // 说明id是合法的，将当前页面加入到 历史足迹 中。
        ShopFootprintUtils.addFootPrint(request.getSession(), id);
        // 展示首页
        PrintHeadByAuthUtils.printHead(request,writer);

        // 展示商品，并可以展示数量
        String action = request.getContextPath() + "/store/addcart";
        writer.println("<div>\n" +
                "    <form action=" + action + " method=\"get\">\n" +
                "        <table>\n" +
                "            <tr>\n" +
                "                <th>品牌</th>\n" +
                "                <td>" + pdetail.getName() + "</td>\n" +
                "            </tr>\n" +
                "            <tr>\n" +
                "                <th>颜色</th>\n" +
                "                <td>" + pdetail.getColor() + "</td>\n" +
                "            </tr>\n" +
                "            <tr>\n" +
                "                <th>类别</th>\n" +
                "                <td>" + pdetail.getCategory() + "</td>\n" +
                "            </tr>\n" +
                "            <tr>\n" +
                "                <th>价格</th>\n" +
                "                <td>" + pdetail.getPrice() + "</td>\n" +
                "            </tr>\n" +
                "            <tr>\n" +
                "                <th>描述</th>\n" +
                "                <td>" + pdetail.getDescription() + "</td>\n" +
                "            </tr>\n" +
                "            <tr>\n" +
                "                <th>数量</th>\n" +
                "                <td><input type=\"number\" name=\"count\" value='1'></td>\n" +
                "            </tr>\n" +
                "            <tr>\n" +
                "                <td>\n" +
                "                    <input type='hidden' name='id' value='" + pdetail.getId() + "'>" +
                "                </td>\n" +
                "            </tr>\n" +
                "            <tr>\n" +
                "                <td align=\"center\">\n" +
                "                    <input type=\"submit\" value=\"添加购物车\" style=\"color: chocolate;font-size: large\">\n" +
                "                </td>\n" +
                "            </tr>\n" +
                "        </table>\n" +
                "    </form>\n" +
                "</div>");
    }
}
